/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MODEL.CONTROLLER;

import MODEL.DAO.*;
import MODEL.POJO.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author kijoo
 */
@WebServlet(name = "DanhSachSanPhamController", urlPatterns = {"/DanhSachSanPhamController"})
public class DanhSachSanPhamController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String action = request.getParameter("Action");
        if (action != null) {
            int trang = Integer.parseInt(request.getParameter("Trang"));
            //if(action.equals("PC")){
            ArrayList<LoaiDanhMucPOJO> loaidanhmuc = LoaiDanhMucDAO.LayDanhSachLoaiDanhMuc(action);
            ArrayList<LocSanPhamPOJO> dslsp = LocSanPhamDAO.LayLoctenmuc(action);
            request.setAttribute("LocSanPhams", dslsp);
            request.setAttribute("LoaiDanhMucs", loaidanhmuc);
            request.setAttribute("LoaiSanPhams", action);
            int soluongtrang = 0;
            String yeucau = request.getParameter("YeuCau");
            if (request.getParameter("idloaisanpham") != null) {
                String idloaisanpham = request.getParameter("idloaisanpham");
                ArrayList<ChiTietSanPhamPOJO> dsctsp = ChiTietSanPhamDAO.LayDSTheoLSPCoPhanTrang(idloaisanpham, trang, yeucau);
                soluongtrang = ChiTietSanPhamDAO.tinhsoluongtrang5(idloaisanpham);
                int xuly = 5;
                request.setAttribute("XyLy", xuly);
                request.setAttribute("SoTrang", soluongtrang);
                request.setAttribute("YeuCau", yeucau);
                request.setAttribute("DanhSachCTSPs", dsctsp);
                request.getRequestDispatcher("/SanPham.jsp").forward(request, response);
            } else if (request.getParameter("idloaidanhmuc") != null) {
                String idloaidanhmuc = request.getParameter("idloaidanhmuc");
                ArrayList<DanhMucPOJO> dsdm = DanhMucDAO.LayDanhSachDanhMuc(idloaidanhmuc);
                LoaiDanhMucPOJO ldm = LoaiDanhMucDAO.LayLoaiDM(idloaidanhmuc);
                ArrayList<ChiTietSanPhamPOJO> dsctsp = ChiTietSanPhamDAO.LayDSTheoLDMCoPhanTrang(idloaidanhmuc, trang, yeucau);
                soluongtrang = ChiTietSanPhamDAO.tinhsoluongtrang1(idloaidanhmuc);
                int xuly = 1;
                request.setAttribute("SoTrang", soluongtrang);
                request.setAttribute("XyLy", xuly);
                request.setAttribute("YeuCau", yeucau);
                request.setAttribute("LoaiDM", ldm);
                request.setAttribute("DanhSachCTSPs", dsctsp);
                request.setAttribute("DanhSachDMs", dsdm);
                request.getRequestDispatcher("/SanPham.jsp").forward(request, response);
            } else if (request.getParameter("iddanhmuc") != null) {
                String iddanhmuc = request.getParameter("iddanhmuc");
                ArrayList<SanPhamPOJO> dssp = SanPhamDAO.LayDanhSachSanPham(iddanhmuc);
                DanhMucPOJO dm = DanhMucDAO.LayTenDM(iddanhmuc);
                LoaiDanhMucPOJO ldm = DanhMucDAO.LayTenLoaiDM(iddanhmuc);
                ArrayList<ChiTietSanPhamPOJO> dsctsp = ChiTietSanPhamDAO.LayDSTheoDMCoPhanTrang(iddanhmuc, trang, yeucau);
                soluongtrang = ChiTietSanPhamDAO.tinhsoluongtrang2(iddanhmuc);
                int xuly = 2;
                request.setAttribute("SoTrang", soluongtrang);
                request.setAttribute("XyLy", xuly);
                request.setAttribute("YeuCau", yeucau);
                request.setAttribute("DM", dm);
                request.setAttribute("LoaiDM", ldm);
                request.setAttribute("DanhSachCTSPs", dsctsp);
                request.setAttribute("DanhSachDMs", dssp);
                request.getRequestDispatcher("/SanPham.jsp").forward(request, response);
            } else if (request.getParameter("idsanpham") != null) {
                String idsanpham = request.getParameter("idsanpham");
                SanPhamPOJO sp = SanPhamDAO.LaySP(idsanpham);
                DanhMucPOJO dm = DanhMucDAO.LayDMTheoSP(idsanpham);
                LoaiDanhMucPOJO ldm = LoaiDanhMucDAO.LayLoaiDMTheoSP(idsanpham);
                ArrayList<ChiTietSanPhamPOJO> dsctsp = ChiTietSanPhamDAO.LayDSTheoSPCoPhanTrang(idsanpham, trang, yeucau);
                soluongtrang = ChiTietSanPhamDAO.tinhsoluongtrang3(idsanpham);
                int xuly = 3;
                request.setAttribute("SoTrang", soluongtrang);
                request.setAttribute("XyLy", xuly);
                request.setAttribute("YeuCau", yeucau);
                request.setAttribute("TenSanPham", sp);
                request.setAttribute("DM", dm);
                request.setAttribute("LoaiDM", ldm);
                request.setAttribute("DanhSachCTSPs", dsctsp);
                request.setAttribute("DanhSachDMs", null);
                request.getRequestDispatcher("/SanPham.jsp").forward(request, response);
            } //locsanpham
            else if (request.getParameter("giatu") != null) {
                int Trang = Integer.valueOf(request.getParameter("Trang"));
                String text = (String) request.getParameter("Text");
                BigDecimal giatu = new BigDecimal(request.getParameter("giatu"));
                BigDecimal giaden = new BigDecimal(request.getParameter("giaden"));
                ArrayList<ChiTietSanPhamPOJO> dsctsp = ChiTietSanPhamDAO.LocSPTheoGia(action, giatu, giaden, Trang, yeucau);
                soluongtrang = ChiTietSanPhamDAO.tinhsoluongtrang4(action, giatu, giaden);
                int xuly = 4;
                request.setAttribute("SoTrang", soluongtrang);
                request.setAttribute("XyLy", xuly);
                request.setAttribute("YeuCau", yeucau);
                request.setAttribute("Text", text);
                request.setAttribute("giatu", request.getParameter("giatu"));
                request.setAttribute("giaden", request.getParameter("giaden"));
                request.setAttribute("DanhSachCTSPs", dsctsp);
                request.getRequestDispatcher("/SanPham.jsp").forward(request, response);
            }
            //}
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
